package sword_to_1;

public class LeeCode_13 {
    public static void main(String[] args) {
        System.out.println(movingCount(16, 8, 4));
    }
    private static int movingCount(int m, int n, int k) {
        if (k == 0)
            return 1;
        int ans = 1;
        boolean[][] can = new boolean[m][n];
        can[0][0] = true;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 && j == 0){
                    continue;
                }
                boolean flag = i - 1 >= 0 && can[i - 1][j];
                if (j - 1 >= 0 && can[i][j - 1]){
                    flag = true;
                }
                if (!flag){
                    continue;
                }
                int t = 0;
                int ti = i;
                int tj = j;
                while (ti != 0){
                    t += ti % 10;
                    ti /= 10;
                }
                while (tj != 0){
                    t += tj % 10;
                    tj /= 10;
                }
                if (t <= k){
                    ans++;
                    can[i][j] = true;
                }
            }
        }
        return ans;
    }
}
